<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 监测设备管理
 * @package app\Detection
 * @author LJQ<245369209@qq.com>
 */
class Detection extends Api

{

    /**
   * 监测设备列表
   * @param string DetectionId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function detectionList($scope = null){
		$where = [] ;
		if($scope){
			
			$where['enterpriseId'] = ['in',$scope];
		}
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//监测设备列表
			$info =collection(model('Detection')->with('enterprise,dangerSource')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//监测设备列表
			$info =collection(model('Detection')->with('enterprise,dangerSource')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Detection')->where($where)->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到监测设备','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'监测设备信息','count'=>$count,'data'=>$info]);
       		}
        return json(['code'=>0,'msg'=>'数据异常']);
    }
   	  
   	      //添加监测设备
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Detection');
          $result = $model->change($data);
          if (false != $result) {
             setlog('detectionadd','detection',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'监测设备添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'监测设备添加失败,请检查']);
          }
      }

   	

   /**
   * 修改监测设备
   * @param  string DetectionId 监测设备id,
   * @return Json
   */
   	public function edit($data,$detectionId){
        
          if (empty($detectionId)) {
              return json(['code'=>0,'msg'=>'监测设备不存在,请检查']);
          }
          $model = model('Basic/Detection');     
          if ($model->change($data)) {
             setlog('detectionupdate','detection',session('user_auth.id'));

             return json(['code'=>1,'msg'=>'监测设备信息修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'监测设备信息修改失败,请检查']);
            }
    }
    /**
   *  删除监测设备
   * @param string DetectionId 监测设备id,
   * @param Json
   */
     
    public function del($detectionId) {
      	 
        $model = model('Basic/Detection');
      	if (empty ( $detectionId )||empty(($model->where(array('id'=>$detectionId))->find()))) {
           	return  json(['code'=>0,'msg'=>'监测设备id不存在,请检查']);
        }
        if($model->destroy(['id'=>$detectionId]) !== FALSE){
           setlog('detectiondel','detection',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'监测设备删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'监测设备删除失败,请检查']);
       }
    }

     
   /**
   *   查询监测设备
   * @param string enterpriseId 企业id
   * @param sting danger危险源id 
   * @return Json
   */
   	public function search($code = null,$enterpriseId=null){
   		$where = [] ;

		if($enterpriseId){
			
			$where['enterpriseId'] = ['eq',$enterpriseId];
		}

		if($code){
			$where['cameraCode'] =['like','%'.$code.'%'];
		}


   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//监测设备列表
			$info =collection(model('Detection')->with('enterprise,dangerSource')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//监测设备列表
			$info =collection(model('Detection')->with('enterprise,dangerSource')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Detection')->where($where)->count('id'); 
		 
    	       if ($count == 0) {
              	return json(['code'=>1,'msg'=>'没有找到监测设备','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'监测设备信息','count'=>$count,'data'=>$info]);
       	}
      }
      
      /**
       * 监控目标位置
       * @param string $targetId
       * @return Json
       */
      public function getTargetPosition($targetId){
      	$position = db('dangersource')->where('id',$targetId)->value('latitudeLongitude');
      	if($position){
      		return json(['code'=>1,'msg'=>'','data'=>$position]);
      	}else{
      		return json(['code'=>0,'msg'=>'没有对应位置']);
      	}
      }
      
      /**
       * 传感器列表
       * @param string $title
       * @return Json
       */
      public function sensorList($dangerSourceId = null, $title = null){
      	$where = [];
      	if($title){
      		$where['title'] = $title;
      	}
      	if($dangerSourceId){
      		$where['dangerSourceId'] = ['in',[null,$dangerSourceId]];
      	}
		//监测设备列表
		$info =collection(model('Sensor')->where($where)->select())->toArray();
		
	 	//总数
	   	 $count = count($info);
		
    	if ($count == 0) {
         	return json(['code'=>1,'msg'=>'没有找到传感器','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'','count'=>$count,'data'=>$info]);
       	}
        return json(['code'=>0,'msg'=>'数据异常']);
      }
}